package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 套餐管理相关接口
* */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /*
    * 套餐分类查询
    * */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询套餐信息")
    public Result<PageResult> findAllSetmealByPage(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐信息分页查询：{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.findAllSetmealByPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /*
    * 新增套餐操作
    * */
    @PostMapping
    @ApiOperation(value = "新增套餐操作")
    @CacheEvict(cacheNames = "setmeal", key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐：{}", setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    /*
    * 套餐批量删除操作
    * */
    //可以一次删除一个套餐，也可以批量删除套餐
    //起售中的套餐不能删除
    @DeleteMapping
    @ApiOperation(value = "套餐的批量删除操作")
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result deleteSetmealBatch(@RequestParam List<Long> ids){
        log.info("套餐的批量删除：{}", ids);
        setmealService.deleteSetmealBatch(ids);
        return Result.success();
    }

    /*
    * 根据id查询回显套餐信息
    * */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐信息")
    public Result<SetmealVO> findSetmealById(@PathVariable Long id){
        log.info("根据id查询套餐信息：{}", id);
        SetmealVO setmealVO = setmealService.findSetmealById(id);
        return Result.success(setmealVO);
    }

    /*
    * 修改套餐信息
    * */
    @PutMapping
    @ApiOperation(value = "修改套餐信息")
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐信息：{}", setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    /*
    * 修改套餐状态
    * */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "套餐启售、停售")
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result updateSetmealStatus(@PathVariable Integer status, Long id){
        log.info("修改套餐状态：{}{}",status,id);
        setmealService.updateSetmealStatus(status, id);
        return Result.success();
    }

}
